home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-14 | 2.4 KB | 100 lines | [TEXT/BROW] |
- #include "RectUtilities.h"
-
- #define f(a) ((float)(a))
-
- void RectUtil::ScaleRectToFit(Rect* source,Rect* dest)
- {
- long destWidth = RectWidth(*dest);
- long destHeight = RectHeight(*dest);
- long sourceWidth = RectWidth(*source);
- long sourceHeight = RectHeight(*source);
-
-
- if(sourceWidth > destWidth){
- long newHeight = (f(sourceHeight)/f(sourceWidth)) * f(destWidth);
- source->bottom = source->top + newHeight;
- source->right = source->left + destWidth;
- }
-
- sourceWidth = RectWidth(*source);
- sourceHeight = RectHeight(*source);
-
- if(sourceHeight > destHeight){
- long newWidth = (f(sourceWidth)/f(sourceHeight)) * f(destHeight);
- source->bottom = source->top + destHeight;
- source->right = source->left + newWidth;
- }
-
-
- }
-
-
- void RectUtil::ScaleRectToContain(Rect* enclRect,Rect* objRect)
- {
- long enclWidth = RectWidth(*enclRect);
- long enclHeight = RectHeight(*enclRect);
- long objWidth = RectWidth(*objRect);
- long objHeight = RectHeight(*objRect);
-
-
- if(enclWidth < objWidth){
- long newEnclHeight = (f(enclHeight) / f(enclWidth)) * f(objWidth);
- enclRect->bottom = enclRect->top + newEnclHeight;
- enclRect->right = enclRect->left + objWidth;
- }
-
- enclWidth = RectWidth(*enclRect);
- enclHeight = RectHeight(*enclRect);
-
- if(enclHeight < objHeight){
- long newEnclWidth = (f(enclWidth) / f(enclHeight)) * f(objHeight);
- enclRect->bottom = enclRect->top + objHeight;
- enclRect->right = enclRect->left + newEnclWidth;
- }
- }
-
-
-
- void RectUtil::CenterRectToFit(Rect* source,Rect* dest)
- {
- Point center;
-
- center.h = dest->left + RectWidth(*dest)/2;
- center.v = dest->top + RectHeight(*dest)/2;
-
- short sourceHeight = RectHeight(*source);
- short sourceWidth = RectWidth(*source);
-
- source->top = center.v - sourceHeight/2;
- source->bottom = source->top + sourceHeight;
-
- source->left = center.h - sourceWidth/2;
- source->right = source->left + sourceWidth;
- }
-
-
- void RectUtil::CenterRectOnPoint(Rect* theRect,Point center)
- {
- short width = RectWidth(*theRect);
- short height = RectHeight(*theRect);
-
- theRect->top = center.v - height/2;
- theRect->bottom = theRect->top + height;
- theRect->left = center.h - width/2;
- theRect->right = theRect->left + width;
- }
-
-
-
- void RectUtil::LocalToGlobal(Rect* theRect)
- {
- ::LocalToGlobal(&topLeft(*theRect));
- ::LocalToGlobal(&botRight(*theRect));
- }
-
- void RectUtil::GlobalToLocal(Rect* theRect)
- {
- ::GlobalToLocal(&topLeft(*theRect));
- ::GlobalToLocal(&botRight(*theRect));
- }
-